package com.zhongjian.technologycal.mall.activity.main.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.DepthPageTransformer;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.utils.PicassoImageLoader;
import com.zhongjian.technologycal.mall.utils.PicassoRoundTransform;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * Created by wei on 2017/11/23.
 */

public class ProductFragment extends Fragment {
    public static final String ARGS_PAGE = "args_page";

    private Dialog dialog;
    private int mPage;
    TagFlowLayout flowLayout_hot;
    TagFlowLayout flowLayout_shopping;
    private String[] tags0 = new String[]{
            "套餐A", "套餐B", "套餐C", "套餐D", "套餐E",
            "套餐F"

    };
    private String[] tags1 = new String[]{
            "套餐A", "套餐B", "套餐C", "套餐D", "套餐E"

    };



    private String[] urls = {
            "http://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png",
            "http://img.zcool.cn/community/01fca557a7f5f90000012e7e9feea8.jpg",
            "http://img.zcool.cn/community/01996b57a7f6020000018c1bedef97.jpg"
    };
    private List list = Arrays.asList(urls);
    private List arrayList = new ArrayList(list);
    private TagAdapter<String> mAdapter;


    public static ProductFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARGS_PAGE, page);
        ProductFragment fragment = new ProductFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARGS_PAGE);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product, container, false);
        TextView textView = view.findViewById(R.id.textView);
        textView.setText("第" + mPage + "页");
        Banner details_banner = view.findViewById(R.id.details_banner);
        details_banner.setImages(arrayList)
                .setImageLoader(new PicassoImageLoader())
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
                    }
                })
                .start();
        details_banner.setBannerAnimation(DepthPageTransformer.class);

        RelativeLayout rl_product_colege = view.findViewById(R.id.rl_product_colege);
        rl_product_colege.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showBuyDialog();
            }
        });

        return view;
    }

    /**
     * 弹出购买对话框
     */
    private void showBuyDialog() {
        dialog = new Dialog(getActivity(), R.style.Theme_Light_Dialog);
        Window window = dialog.getWindow();
        View viewDialog = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_details_buy, null);

        window.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        window.setWindowAnimations(R.style.dialog_style);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        dialog.setContentView(viewDialog);
        ImageView iv_good_detail_dialog = dialog.findViewById(R.id.iv_good_detail_dialog);
        flowLayout_hot = dialog.findViewById(R.id.flowLayout_buy);
        flowLayout_shopping = dialog.findViewById(R.id.flowLayout_shopping);

        Picasso.get().load("http://seopic.699pic.com/photo/50008/2836.jpg_wh1200.jpg")
                .transform(new PicassoRoundTransform())
                .into(iv_good_detail_dialog);

        dialog.show();
        setDataFlowLayout0();
        setDataFlowLayout1();
    }

    /**
     * 设置套餐数据
     */
    private void setDataFlowLayout0() {
        final LayoutInflater mInflater = LayoutInflater.from(getActivity());
        flowLayout_hot.setAdapter(mAdapter = new TagAdapter<String>(tags0) {
            @Override
            public View getView(FlowLayout parent, int position, String str) {
                TextView tags = (TextView) mInflater.inflate(R.layout.tags_flowlayout_shopping,
                        flowLayout_hot, false);
                tags.setText(str);
                return tags;
            }
        });
        flowLayout_hot.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {

            }
        });

    }
    /**
     * 设置套餐数据
     */
    private void setDataFlowLayout1() {
        final LayoutInflater mInflater = LayoutInflater.from(getActivity());
        flowLayout_shopping.setAdapter(mAdapter = new TagAdapter<String>(tags1) {
            @Override
            public View getView(FlowLayout parent, int position, String str) {
                TextView tags = (TextView) mInflater.inflate(R.layout.tags_flowlayout_shopping,
                        flowLayout_shopping, false);
                tags.setText(str);
                return tags;
            }
        });
        flowLayout_shopping.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {

            }
        });

    }


}
